package com.maixun.lib_choosefile

import android.content.Intent
import com.maixun.lib_choosefile.entity.SelectParams
import com.maixun.lib_choosefile.ui.LocalFileActivity

/**
 * @author：Lmoumou
 * @date：2020/10/9
 */
class SelectionCreator(private val fileSelect: LocalFileSelect) {
    private val mSelectParams: SelectParams by lazy { SelectParams() }

    fun maxSelectable(maxSelectable: Int): SelectionCreator {
        if (maxSelectable < 1) throw IllegalArgumentException("maxSelectable must be greater than or equal to one")
        mSelectParams.maxFileSelectable = maxSelectable
        return this
    }

    fun forResult(requestCode: Int) {
        val activity = fileSelect.getActivity() ?: return

        val intent = Intent(activity, LocalFileActivity::class.java).apply {
            putExtra(LocalFileConst.MAX_NUM, mSelectParams.maxFileSelectable)
        }

        val fragment = fileSelect.getFragment()
        if (fragment != null) {
            fragment.startActivityForResult(intent, requestCode)
        } else {
            activity.startActivityForResult(intent, requestCode)
        }
    }
}